//
//  ByHomeVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByIndexVC.h"

/** 视图 */
#import "ByCoreViewCarousel.h"          // 轮播视图

/** 单元格 */
#import "ByNotificationCell.h"          // 通知cell
#import "ByTitleCell.h"                 // 标题cell
#import "ByContentCell.h"               // 内容cell
#import "ByImageNewsCell.h"             // 图片cell

/** 控制器 */
#import "ByInformationNoticeVC.h"       // 公告
#import "ByDynamicNewsVC.h"             // 新闻动态
#import "ByImageNewsVC.h"               // 图片新闻列表页

/** api */
#import "ByNetNewsApi.h"
#import "ByNetImageApi.h"


@interface ByIndexVC () <UITableViewDataSource, UITableViewDelegate>

/** 视图 */
@property (nonatomic, strong) UITableView *table;
//@property (nonatomic, strong) ByCoreViewCarousel *carouselView;

/** 数据 */
@property (nonatomic, strong) NSMutableArray *noticeAry;            // 公告数组
@property (nonatomic, strong) NSMutableArray *newsAry;              // 新闻动态数组
@property (nonatomic, strong) NSMutableArray *cooperationAry;       // 四方合作数据
@property (nonatomic, strong) NSMutableArray *imageNewsAry;         // 图片新闻数组
@property (nonatomic, strong) NSMutableDictionary *notifyDic;       //


@end

@implementation ByIndexVC

#pragma mark - getter
- (NSMutableArray *)noticeAry {
    if (!_noticeAry) {
        _noticeAry = [[NSMutableArray alloc] init];
    }
    return _noticeAry;
}
- (NSMutableArray *)newsAry {
    if (!_newsAry) {
        _newsAry = [[NSMutableArray alloc] init];
    }
    return _newsAry;
}
- (NSMutableArray *)cooperationAry {
    if (!_cooperationAry) {
        _cooperationAry = [[NSMutableArray alloc] init];
    }
    return _cooperationAry;
}
- (NSMutableArray *)imageNewsAry {
    if (!_imageNewsAry) {
        _imageNewsAry = [[NSMutableArray alloc] init];
    }
    return _imageNewsAry;
}
- (NSMutableDictionary *)notifyDic {
    if (!_notifyDic) {
        _notifyDic = [NSMutableDictionary dictionary];
    }
    return _notifyDic;
}

//- (ByCoreViewCarousel *)carouselView {
//    if (!_carouselView) {
//        _carouselView = [[ByCoreViewCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2) timerDuration:2];
////        _carouselView.imgUrlstrAry = @[@"", @""];
//    }
//    return _carouselView;
//}


#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];

    [self getCarouselImages];

    [self.table.mj_header beginRefreshing];
}

#pragma mark - net
- (void)postQuery {
    
    [self getCarouselImages];
    [self postQueryForNotice];
    [self postQueryForNews];
    [self postQueryForCooperation];
    [self postQueryForImageNews];
}
/** 获取轮播图 */
//- (void)getCarouselImages {
//    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
//    params.position = [NSString stringWithFormat:@"%i", 6045];
//    params.page_num = @"1";
//    params.page_size = @"5";
//
//    [ByNetImageApi imageCarouselGet:params success:^(ByNetBaseResult *result) {
//        /** 设置轮播 */
//        self.carouselView.models = result.data;
//    } failure:^(ByNetBaseResult *result) {
//        
//    }];
//}
/** 获取公告 */
- (void)postQueryForNotice {

    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_NOTICE];
    params.page_num = @"1";
    params.page_size = @"1";

    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        ByNetNewsModel *model = result.data;
        ByNetNewsItemModel *itemModel = [model.results firstObject];
        [self.noticeAry removeAllObjects];
        if (itemModel == nil) {
            return ;
        }
        [self.noticeAry addObject:itemModel];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        
    } failure:^(ByNetBaseResult *result) {
        NSLog(@"%@", result.msg);
    }];
}
/** 获取新闻动态 */
- (void)postQueryForNews {

    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_NEWS];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        
        [self.table.mj_header endRefreshing];

        ByNetNewsModel *model = result.data;
        ARRAR_LOG(model.results);
        [self.newsAry removeAllObjects];
        [self.newsAry addObjectsFromArray:model.results];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:1];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];

    } failure:^(ByNetBaseResult *result) {
        [self.table.mj_header endRefreshing];
    }];
}
/** 获取四方合作 */
- (void)postQueryForCooperation {
    
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_COOPERATION];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        ByNetNewsModel *model = result.data;
        ARRAR_LOG(model.results);
        [self.cooperationAry removeAllObjects];
        [self.cooperationAry addObjectsFromArray:model.results];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:2];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];

    } failure:^(ByNetBaseResult *result) {
    }];
}
/** 获取图片新闻 */
- (void)postQueryForImageNews {
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_IMAGE];
    params.page_num = @"1";
    params.page_size = @"6";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        
        ByNetNewsModel *model = result.data;
        NSLog(@"获取到的图片新闻的个数:%li", model.results.count);
        [self.imageNewsAry removeAllObjects];
        [self.imageNewsAry addObjectsFromArray:model.results];
        
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:3];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];

    } failure:^(ByNetBaseResult *result) {
    }];
}

#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64 - 40) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    /** 头视图 */
    UIView *view = [[UIView alloc] initWithFrame:self.carouselView.bounds];
    [view addSubview:self.carouselView];
    self.table.tableHeaderView = view;
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self postQuery];
    }];
    self.table.mj_header = header;
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return self.newsAry.count + 1;
    }
    if (section == 2) {
        return self.cooperationAry.count + 1;
    }
    if (section == 3) {
        return 1 + 1;
    }
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = FONT(14);
    }
    
    /** 通知 */
    if (indexPath.section == 0) {
        ByNotificationCell *cell = [ByNotificationCell createCellInTableview:tableView];
        ByNetNewsItemModel *itemModel = [self.noticeAry firstObject];
        cell.contentLabel.text = itemModel.post_title;
        return cell;
    } else {
        NSArray *titles = @[@"新闻动态", @"四方合作单位", @"图片新闻"];
        /** row = 0 的单元格是标题单元格 */
        if (indexPath.row == 0) {
            ByTitleCell *cell = [ByTitleCell createCellInTableview:tableView];
            cell.titleLabel.text = titles[indexPath.section - 1];
            return cell;
        } else {
            /** 新闻动态 */
            if (indexPath.section == 1) {
                static NSString *cellIdentifier = @"newsCell";
                ByContentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                if (cell == nil) {
                    cell = [[ByContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                    cell.cellType = ByContentCellTypeValue1;
                }
                cell.model = self.newsAry[indexPath.row - 1];
                return cell;
            }
            /** 四方合作单位 */
            if (indexPath.section == 2) {
                static NSString *cellIdentifier = @"cooCell";
                ByContentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                if (cell == nil) {
                    cell = [[ByContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                    cell.cellType = ByContentCellTypeValue3;
                }
                cell.model = self.cooperationAry[indexPath.row - 1];
                return cell;
            }
            /** 图片新闻 */
            if (indexPath.section == 3) {
                ByImageNewsCell *cell = [ByImageNewsCell createCellInTableview:tableView];
                cell.models = self.imageNewsAry;
                return cell;
            }
        }
    }

    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /** 每组的第一个单元格都是高度40 */
    if (indexPath.row == 0) {
        return 40;
    } else {
        if (indexPath.section == 1) {
            return 55;
        }
        if (indexPath.section == 2) {
            return 55;
        }
        if (indexPath.section == 3) {
            CGFloat width = (SCREEN_WIDTH - 25) / 2;
            CGFloat height = width + 30;
            NSInteger rows = self.imageNewsAry.count / 2 + (self.imageNewsAry.count % 2);
            return (rows == 0 ? 0 : 10 + height * rows + 10);
        }
    }
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 0.5;
    } else {
        return 5;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    /** 跳转至新闻公告 */
    if (indexPath.section == 0) {
        ByInformationNoticeVC *vc = [[ByInformationNoticeVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    /** 跳转至新闻动态列表 */
    if (indexPath.section == 1 && indexPath.row == 0) {
        ByDynamicNewsVC *vc = [[ByDynamicNewsVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    /** 跳转至四方合作（待完成） */
    if (indexPath.section == 2 && indexPath.row == 0) {
        return;
    }
    /** 跳转至图片新闻列表 */
    if (indexPath.section == 3 && indexPath.row == 0) {
        ByImageNewsVC *vc = [[ByImageNewsVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    ByNetNewsItemModel *model = nil;
    if (indexPath.section == 1) {
        model = self.newsAry[indexPath.row - 1];
    }
    if (indexPath.section == 2) {
        model = self.cooperationAry[indexPath.row - 1];
    }
    if (indexPath.section == 3) {
        return; 
    }
    NSString *postID = model.id;
    NSString *urlStr = [NSString stringWithFormat:@"Webview/Post/viewPost?id=%@", postID];
    ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:urlStr];
    vc.navigationItem.title = model.post_cate_name;
    [self.navigationController pushViewController:vc animated:YES];

}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
